package com.mybatis.test;
import com.mybatis.test.dao.StudentDao;
import com.mybatis.test.entities.Student;
import com.mybatis.test.spring.config.MyBatisConfig;
import com.mybatis.test.spring.entities.User;
import com.mybatis.test.spring.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @date 2020-08-11 上午10:52
 */
public class App {
    private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        try (final SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE)) {
            final StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            final Student student = mapper.selectStudentById(1);
            LOGGER.warn(student.toString());
        }

        ApplicationContext context = new AnnotationConfigApplicationContext(MyBatisConfig.class);
        UserMapper userMapper = context.getBean(UserMapper.class);
        User user = userMapper.getUser(2);
        LOGGER.info(user.toString());

        User user1 = new User();
        user1.setName("test");
        user1.setAge("15");
        user1.setBirth(LocalDate.now());
        userMapper.insertUser(user1);
        LOGGER.info(user1.toString());

        User user3 = userMapper.getUser(2);
        LOGGER.info(user3.toString());
    }
}
